package com.anzhen.interview.lang;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WriteFile {
    public static void main(String[] args) throws IOException {
        String str = FileUtils.readFileToString(new File("/Users/zhenan/Desktop/index.json"), "UTF8");
        Root root = JSON.parseObject(str, Root.class);
        List<Indexes> indexes = root.getIndexes();
        HashMap<Integer, IndexDatas> map = new HashMap<>();
        ArrayList<Integer> hashcodeList = new ArrayList<>();
        for (Indexes index : indexes) {
            for (IndexDatas indexDatas : index.getIndexDatas()) {
                int i = indexDatas.hashCode();
                if (!map.containsKey(i)) {
                    map.put(i, indexDatas);
                    hashcodeList.add(i);
                }
            }
        }

        for (int i = 0; i < 1000000; i++) {
            List<Indexes> list = new ArrayList<>();
            Indexes index = new Indexes();
            List<IndexDatas> indexDatas = new ArrayList<>();
            int size = RandomUtils.nextInt(10, 20);
            for (int j = 0; j < size; j++) {
                IndexDatas e = map.get(hashcodeList.get(RandomUtils.nextInt(0, hashcodeList.size())));
                indexDatas.add(e);
            }
            index.setIndexDatas(indexDatas);
            list.add(index);
            FileUtils.writeStringToFile(new File("/Users/zhenan/Desktop/big.json"), JSON.toJSONString(list), "UTF8",true);
        }
    }
}
